// pages/category/index.js
const api = require('../../utils/api')
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopObj:{
            title:"豆豆新能源汽车",
            logo:"/images/logo.png",
            name:"客服",
            phone:"02867871188"
        },
        isFull:false,
        address:"地址1234",
        phone:"15528353063",
        wx:"cbq123",
        option1: [
            { text: '智能排序', value: 0 },
            { text: '车龄最大', value: 1 },
            { text: '车龄最小', value: 2 },
            { text: '价格最低', value: 4 },
            { text: '价格最高', value: 5 },
            { text: '里程最大', value: 6 },
            { text: '里程最小', value: 7 },
          ],
          option2: [
            { text: '车型', value: 'a' },
            { text: '小面', value: 'b' },
            { text: '中面', value: 'c' },
            { text: '大面', value: 'd' },
            { text: '微面', value: 'e' },
            { text: '4.2m货车', value: 'f' },
            { text: '微卡', value: 'g' },
          ],
          option3: [
            { text: '价格', value: 'a' },
            { text: '3万以下', value: 'b' },
            { text: '3-6万', value: 'c' },
            { text: '6-10万', value: 'd' },
            { text: '10万以上', value: 'e' },
          ],
          option4: [
            { text: '筛选', value: 'a' },
          ],
          value1: "",
          value2: "",
          value3:"",
          idList:[],
          tabList:[],
          carList:[],
          page:1,
          isShare:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // let uid = options.uid || wx.getStorageSync('uid')
        // console.log(uid,'uid')
        // wx.setStorageSync('uid', uid)
        if( options.uid || wx.getStorageSync('uid')){
            this.setData({
                isShare:true
            })
            let uid = options.uid || wx.getStorageSync('uid')
            wx.setStorageSync('uid', uid)
            // 业务员分享分享进入
            let obj = {
                opcode:"Int16Array",
                id: parseInt(uid)
            }
            api.userinfo({
                data:obj,
                method:'post',
                success: (res)=>{
                   console.log(res)
                    this.setData({
                        'shopObj.logo':res.data.data.data[0].imgpath,
                        'shopObj.name':res.data.data.data[0].name,
                        'shopObj.phone':res.data.data.data[0].phone,
                    })
                }
            })

        }else{
            console.log('noshare')
        }
        this.getCategoryList()
        this.getCarList()
        this.setViewList()

    },
    handlePhone(){
        let phone = this.data.shopObj.phone ||   "02867871188"
        wx.makePhoneCall({
            phoneNumber: phone 
        })
    },
    handleImg(){
        wx.previewImage({
            urls: [this.data.shopObj.logo]
         })
    },
    setViewList(){
        let result =  wx.getStorageSync('viewList')
        if(!result){
            wx.setStorageSync('viewList', [])
        }
      },
    getCategoryList(){
        this.setData({
            idList:wx.getStorageSync('idList')||[]
        })
        // 初始化车辆
        api.getCategoryList({
            data: {
                opcode: "1",
                pagesize: 10,
                page: 1,
              }
            ,
            success:(res)=>{
                let data = res.data.data.data
                var category = {
                    1001: "车型",
                    1002: "品牌",
                    1003: "价格",
                    1004: "公里数",
                    1005: "车龄",
                  };
                  let dataList = new Array();
                  let child = new Array();
                  let dataDict = {};
                  for (let k in data) {
                    dataDict["id"] = Number(k);
                    dataDict["name"] = category[k];
                    for (let j in data[k]) {
                      data[k][j] = JSON.parse(data[k][j]);
                      data[k][j]["id"] = Number(j);
                      child.push(data[k][j]);
                    }
                    dataDict["child"] = child;
                    dataList.push(dataDict);
                    dataDict = {};
                    child = [];
                  }
                    let newArr = []
                    for(let i=0 ;i<dataList.length;i++){
                        let option1 = dataList[i].child.map((item)=>{
                            return {
                                text:item.nickname,
                                value:item.id
                            }
                        })
                        newArr.push(option1)
                    }
                    this.setData({
                        tabList:newArr
                    })
            }
        })
    },
    getSearch(){
        let obj = {
            opcode: "Int16Array",
            children: this.data.idList,
            page: 1,
            pagesize: 10,
        }
        api.categorysearch({
            data:obj,
            method:'post',
            success: (res)=>{
                if(res.data.data.data){
                    this.setData({
                        carList:res.data.data.data
                    })
                }else{
                    this.setData({
                        carList:[]
                    })
                }
                
            }
        })
    },
    getCarList(){
        let obj = {
            opcode: "Int16Array",
            page: this.data.page,
            pagesize: 10,
        }
        api.getCarList({
            data:obj,
            method:'post',
            success: (res)=>{
                this.setData({
                    carList:res.data.data.data
                })
            }
        })
    },
    handleSearch(e){
    //    this.selectComponent("#screen").toggle(false)
        wx.navigateTo({
          url: '../screen/index',
        })
    },
        //复制订单号
        copywxtap: function (e) {
        console.log(e.currentTarget.dataset.text)
        wx.setClipboardData({
            data: e.currentTarget.dataset.text,
            success: function (res) {
            wx.getClipboardData({
                //这个api是把拿到的数据放到电脑系统中的
                success: function (res) {
                console.log(res.data) // data
                wx.showToast({
                    title: '复制成功',
                })
                }
            })
            }
        })
        },
    onReachBottom: function(){
        let that = this
        let page = that.data.page
        page = page+1
        let obj = {
            opcode: "Int16Array",
            page: page,
            pagesize: 10,
        }
        if(!that.data.isFull){
            api.getCarList({
                data:obj,
                method:'post',
                success: (res)=>{
                    if(res.data.data.data){
                        this.setData({
                          carList:that.data.carList.concat(res.data.data.data),
                          page:page
                      })
                      }else{
                        wx.showToast({
                          title: '已显示最多数据',
                        })
                        this.setData({
                            isFull: true
                        })
                      }
                }
            })
        }
    },
    changeOption1(e){
        this.setData({
            idList:[]
        })
        this.data.idList.push(e.detail)
        this.setData({
            idList:this.data.idList
        })
        wx.setStorageSync('idList', this.data.idList)
        this.getSearch()
    //     if(!this.data.value1){
    //         this.setData({
    //             value1:e.detail
    //         })
    //     }else{
    //         this.setData({
    //             value1:""
    //         })
    //     }
    //   if(this.data.idList.includes(e.detail)){
    //       let index = this.data.idList.indexOf(e.detail)
    //       this.setData({
    //           idList:this.data.idList.splice(index,1)
    //       })
    //       wx.setStorageSync('idList', this.data.idList)
    //   }else{
    //       this.data.idList.push(e.detail)
    //       this.setData({
    //           idList:this.data.idList
    //       })
    //       wx.setStorageSync('idList', this.data.idList)
    //   }
    },
    changeOption2(e){
        this.setData({
            idList:[]
        })
        this.data.idList.push(e.detail)
        this.setData({
            idList:this.data.idList
        })
        wx.setStorageSync('idList', this.data.idList)
        this.getSearch()
    },
    changeOption3(e){
        this.setData({
            idList:[]
        })
        this.data.idList.push(e.detail)
        this.setData({
            idList:this.data.idList
        })
        wx.setStorageSync('idList', this.data.idList)
        this.getSearch()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            idList:wx.getStorageSync('idList')
        })
        if(app.globalData.isSearch==true){
            this.getSearch()
            app.globalData.isSearch=false
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getCarList()
        this.setData({
            isFull: false
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let uid = wx.getStorageSync('uid')
        // let userid = wx.getStorageSync('id')
        // let phone = wx.getStorageSync('phone')
      return {
        title: "豆豆新能源汽车小程序",
        path: 'pages/category/index?uid='+uid
      }
    }
})